<?php
/**
 * User: ailuoga
 * Created Time: 2017/1/28 15:13
 * Description :
 */


Route::get('redis', function () {
    $value=app('redis.client')->get('mykey');
    dd($value);
});

Route::get('captcha', function () {
    return view('partial.captcha');
});

Route::get('sms', function () {
    $url='https://rest.nexmo.com/sms/json';
    $client = new \GuzzleHttp\Client();

    $res=$client->request('GET', $url, [
        'query' => [
            'api_key'=>'3b32e905',
            'api_secret'=>'f3ddff22cf91f803',
            'to'=>8613128107501,
            'from'=>"NEXMO",
            'text'=>"Hello from Nexmo"
        ]
    ]);
//    $res=$client->get($url,[
//        'query'=>[
//            'api_key'=>'3b32e905',
//            'api_secret'=>'f3ddff22cf91f803',
//            'to'=>"8613128107501",
//            'from'=>"NEXMO",
//            'text'=>"Hello from Nexmo"
//        ],
//    ]);
    dd($res);

    $request= new \GuzzleHttp\Psr7\Request('GET',$url);
//        $this->client->send($request);
//        $this->client->request()
    echo 'sending...';
    $promise = $client->send($request,[
        'query'=>[
            'api_key'=>'3b32e905',
            'api_secret'=>'f3ddff22cf91f803',
            'to'=>"8613128107501",
            'from'=>"NEXMO",
            'text'=>"Hello from Nexmo"
        ],
    ])->then(function ($response) {
        echo 'I completed! ' . $response->getBody();
    });
});

Route::get('kit/captcha/{tmp}',function (){
    $phrase = new PhraseBuilder;

    $code = $phrase->build(4);// 设置验证码位数
    $builder = new CaptchaBuilder($code, $phrase);// 生成验证码图片的Builder对象，配置相应属性
    $builder->setBackgroundColor(220, 210, 230);
    $builder->setMaxAngle(25);
    $builder->setMaxBehindLines(0);
    $builder->setMaxFrontLines(0);
    $builder->build($width = 80, $height = 40, $font = null);

    $phrase = $builder->getPhrase();// 获取验证码的内容
    // 把内容存入session
    \Session::flash('code', $phrase);
    // 生成图片
    header("Cache-Control: no-cache, must-revalidate");
    header("Content-Type:image/jpeg");
    $builder->output();
//    $userInput = \Request::get('captcha');
//    if (Session::get('code') == $userInput) {
//        //用户输入验证码正确
//        return '您输入验证码正确';
//    } else {
//        //用户输入验证码错误
//        return '您输入验证码错误';
//    }

});

Route::get('image', function () {
//    dd(upload_path('img_origin/sea.jpg'));
//    dd(File::isFile(upload_path('img_origin/sea.jpg')));
//    dd( File::exists(upload_path('img_origin/sea.jpg')));
//    $img=app('image')->make(upload_path('img_origin/sea.jpg'));
//    $img->resize(320, 240);
//    $img->insert('public/watermark.png');
// finally we save the image as a new file
//    $img->save(upload_path('img_thumb/sea_thumb.jpg'));
//    dd($img);
    $img = Image::make(upload_path('img_origin/sea01.jpg'))->resize(320, 240)
        ->save(upload_path('img_thumb/sea01_thubm.jpg'));
    dd($img);

});



//chat
Route::get('chat', function () {
    return view('chat.chat');
});
//Route::post('sendmessage', 'ChatController@sendMessage');

Route::get('/test_post/{id}', function ($id) {
    if ($id) {
        $current = Post::find($id);
        $previousId = Post::where('id', '<', $id)->max('id');
        $nextId = Post::where('id', '>', $id)->min('id');
//        $previousId = Post::getp
//        dd($previousPostID);
        $uri_previous = url('posts/' . $previousId);
        $uri_next = url('posts/', $nextId);
//        return "<a class='btn btn-default' href='".url('posts/'.$previousId)."'>previous_$previousId</a>"
//            ."<a href='".url('posts/'.$nextId)."'>next_$nextId</a>";
        return <<<END
            <a href="$uri_previous">previousId_$previousId</a>
            <a href="$uri_next">previousId_$nextId</a>
END;

    }
})->name('test_post');


Route::get('/home', 'HomeController@index');
Route::get('/test', function () {
    return view('layouts.app');
});

//Route::get('/home','PostController@login')

Route::get('/artisan', function () {
    return Artisan::call('db:seed');
});

Route::get('logs', '\Rap2hpoutre\LaravelLogViewer\LogViewerController@index');

Route::get('/upload/index', function () {
    return view('upload.index');
});

Route::get('repo/post', function () {
    $post=app('post.repository')->find(2);
    dd($post);
});

/**
 * test ajax
 */
Route::get('ajax', function () {
    return view('message');
});
Route::get('/getmsg', function () {
    $msg = "这是一条简单的消息.";
    return response()->json(array('msg' => $msg), 200);
});

Route::get('/guzzle1', function () {
    return app('thirdApi.service')->getContent();
});

/**
 * test guzzle
 */
Route::get('/guzzle', function () {
    $client = new \GuzzleHttp\Client();
//    $res=$client->get('http://www.baidu.com');
//    dd($res->get);
//    echo $res->getStatusCode();
//    echo $res->getHeader('content-type')[0];
//    echo $res->getBody();
//    return $res->getBody();
    // Send an asynchronous request.
    $request = new \GuzzleHttp\Psr7\Request('GET', 'http://httpbin.org');
    $promise = $client->sendAsync($request)->then(function ($response) {
        /**
         * @var $response \GuzzleHttp\Psr7\Response
         */
        echo 'I completed! ' . $response->getBody();
    });
    $promise->wait();
});
/**
 * upload file
 */

Route::get('/upload/show', function () {
//    DB::table('user')->whereDate('created_at','2016-10-20');
    return view('upload.shows');
});

Route::get('down', function () {
    $myFile = public_path("dummy_pdf.pdf");
    $headers = ['Content-Type: application/pdf'];
    $newName = 'itsolutionstuff-pdf-file-' . time() . '.pdf';
    return response()->download($myFile, $newName, $headers);
});

Route::post('/upload', function () {
    $image = \Request::file('image');
    $this->validate($image, [
        'image' => 'required|image|mimes:jpeg,png,jpg,gif,svg|max:2048',
    ]);
    if ($image->isValid()) {
        $destinationPath = public_path() . '/uploads/';
        $filename = $image->getClientOriginalName()
            . "-" . \Carbon\Carbon::now()
            . "." . $image->getClientOriginalExtension();
//        $size=$image->getSize();
        $upload_success = $image->move($destinationPath, $filename);
        if ($upload_success) {
//            \Image::make($destinationPath . $filename)->resize(100, 100)->save($destinationPath . "100x100_" . $filename);
            return Response::json('success', 200);
        } else {
            return Response::json('error', 400);
        }
    }
    return back()
        ->with('success', 'Image Uploaded successfully.')
        ->with('path', $image->getClientOriginalName());
});

Route::post('uploads', function () {
    /**
     * @var $files array
     */
    $files = \Request::file('files');
    $json = array(
        'files' => array()
    );
    if (isset($files) && count($files) > 0) {
        foreach ($files as $file):
            /**
             * @var $file \Symfony\Component\HttpFoundation\File\UploadedFile
             */
            $filename = $file->getClientOriginalName() . "." . $file->getClientOriginalExtension();
            $json['files'][] = array(
                'name' => $filename,
                'size' => $file->getSize(),
                'type' => $file->getMimeType(),
                'url' => '/uploads/files/' . $filename,
                'deleteType' => 'DELETE',
//            'deleteUrl' => self::$route.'/deleteFile/'.$filename,
            );
            $upload = $file->move(public_path() . '/uploads/files', $filename);
        endforeach;
    }
    return Response::json($json);
});